package com.edu.zut.cs.controller;

import com.edu.zut.cs.Utils.QiniuUtil;
import com.edu.zut.cs.pojo.Result;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/files")
public class UploadFilesQiNiuController {
    @Autowired
    QiniuUtil qiniuUtil;

    @ApiOperation("文件上传")
    @PostMapping("/images")
    public Result<?> uploadImages(@RequestParam("fileup") MultipartFile file) throws Exception {
        Result<?> result = new Result<>();
        String url = "";
        if (file == null) {
            return Result.error("403","上传错误");
        }
        try {
            byte[] bytes = file.getBytes();//将文件转化为 byte[] 格式
            String name = UUID.randomUUID().toString().replace("-", "");//更改后的文件名
            String ext = FilenameUtils.getExtension(file.getOriginalFilename());//获取文件后缀
            String ur = name + "." + ext;
            url = qiniuUtil.uploadBybyte(bytes, ur);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //System.out.println(url + " 图片地址");
        return Result.success(url);

    }
}


